package com.wangshb.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangshb.upms.core.utils.AssertUtil;
import com.wangshb.upms.entity.UpmsRolePermission;
import com.wangshb.upms.mapper.UpmsRolePermissionMapper;
import com.wangshb.upms.service.UpmsRoleService;
import com.wangshb.upms.service.UpmsRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service
@Slf4j
public class UpmsRolePermissionServiceImpl extends ServiceImpl<UpmsRolePermissionMapper, UpmsRolePermission> implements UpmsRolePermissionService {

    @Resource
    private UpmsRolePermissionMapper UpmsRolePermissionMapper;

    @Autowired
    private UpmsRoleService upmsRoleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void allocatePermissions(Long roleId, List<Long> permissionIdList) {

        Collection<Long> addPermissionIdList = null;
        Collection<Long> delPermissionIdList = null;

        // 查询已经拥有的权限
        QueryWrapper<UpmsRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("upms_role_id", roleId);
        List<UpmsRolePermission> dbUpmsRolePermissions = this.UpmsRolePermissionMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(dbUpmsRolePermissions)) {

            Set<Long> hadPermissionIdList = dbUpmsRolePermissions.stream().map(UpmsRolePermission::getUpmsPermissionId).collect(Collectors.toSet());

            // 移除已经存在的权限
            addPermissionIdList = CollectionUtils.removeAll(permissionIdList, hadPermissionIdList);
            delPermissionIdList = CollectionUtils.removeAll(hadPermissionIdList, permissionIdList);

            if (CollectionUtils.isEmpty(addPermissionIdList)) {
                log.warn("用户{} - 分配权限 - 无新增权限!", roleId);
            }

            if (CollectionUtils.isEmpty(delPermissionIdList)) {
                log.warn("用户{} - 分配权限 - 无删除权限!", roleId);
            }
        }
        else {
            addPermissionIdList = permissionIdList;
        }

        // 删除
        if (CollectionUtils.isNotEmpty(delPermissionIdList)) {
            UpdateWrapper<UpmsRolePermission> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("upms_permission_id", delPermissionIdList);
            this.UpmsRolePermissionMapper.delete(updateWrapper);
        }

        // 新增
        List<UpmsRolePermission> saveUpmsRolePermission = new ArrayList<>(permissionIdList.size());
        if (CollectionUtils.isNotEmpty(addPermissionIdList)) {
            addPermissionIdList.stream().forEach(permissionId -> {
                saveUpmsRolePermission.add(UpmsRolePermission.builder().upmsRoleId(roleId).upmsPermissionId(permissionId).build());
            });
            // 批量插入
            super.saveBatch(saveUpmsRolePermission, saveUpmsRolePermission.size());
        }
    }


    @Override
    public List<Long> listPermissionByRoleId(Long roleId) {

        // 查询已经拥有的权限
        QueryWrapper<UpmsRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("upms_role_id", roleId).orderByAsc("id");
        List<UpmsRolePermission> dbUpmsRolePermissions = this.UpmsRolePermissionMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(dbUpmsRolePermissions)) {
            return dbUpmsRolePermissions.stream().map(UpmsRolePermission::getUpmsPermissionId).collect(Collectors.toList());
        }

        return Collections.emptyList();
    }
}
